#include "slScene.hh"

#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <fstream>

slScene::slScene(World *w, slInput *i, slCamera *c) :
world(w), input(i), camera(c)
{
	for (int i = 1; i < 40; i += 2)
		v.push_back(new Box(0.0f, i, 0.0f));

	for (int i = 1; i < 40; i += 2)
		v.push_back(new Box(0.0f, i, 0.0f));

	for (int i = 1; i < 40; i += 2)
		v.push_back(new Box(0.0f, i, 0.0f));

	//texture.loadTexture("textures/box.jpg");

	for (std::vector<slPrimitive*>::iterator i = v.begin(); i != v.end(); ++i)
	{
		world->addRigidBody((*i)->getRigidBody());
		(*i)->setColor((float)(slRand(100) / 100), (float)(slRand(100) / 100), (float)(slRand(100) / 100));
		//i->setTexture(texture.getID());
		//i->enableTexture();
	}

	light.SetPosition(0.0f, 20.0f, 0.0f, 1.0f);
	light.SetDiffuseColor(0.4f, 0.4f, 0.4f, 1.0f);
	light.SetSpecular(0.4f, 0.4f, 0.4f, 1.0f);
	light.SetAmbientColor(0.4f, 0.4f, 0.4f, 0.4f);
	GLfloat mod_ambt[] = {0.4F,0.4F,0.4F,1.0F};
	GLfloat local_view[] = {0.0F}; 
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,mod_ambt); 
	glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER,local_view);
	glClearColor(0.0f, 0.1f, 0.1f, 0.0f);
	light.Init(GL_LIGHT0);
	shader.loadShader("shaders/shader.vert", "shaders/shader.frag");
}

void slScene::checkMouse()
{
	if (input->getMouseButton(1))
	{
		slPrimitive *s = new slSphere(camera->getPosition().x, camera->getPosition().y, camera->getPosition().z, 1);
		s->getRigidBody()->setLinearVelocity(btVector3(camera->getOrientation().x, camera->getOrientation().y, camera->getOrientation().z )  * 100);
		world->addRigidBody(s->getRigidBody());
		v.push_back(s);
	}

}


void slScene::draw()
{

	checkMouse();
	shader.begin();
	world->draw(0);
	shader.end();

	for (std::vector<slPrimitive*>::iterator i = v.begin(); i != v.end(); ++i)
	{
		shader.begin();	
		(*i)->draw();
		shader.end();
	}
		light.draw();
}

slScene::~slScene()
{

}